"""
Copyright 2009 Texas A&M University
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License. You may obtain a copy of the 
License at

http://www.apache.org/licenses/LICENSE-2.0
  
Unless required by applicable law or agreed to in writing, software distributed 
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License. 
"""

from ftplib import FTP
import re
from tempfile import TemporaryFile
from logging import getLogger

from zope.interface import implements

from meercat.harvester.base.interfaces import IFileSource



log = getLogger('meercat.harvester.base.sources')

class FileWriteCallback():
    def __init__(self, file):
        self._file = file

    def __call__(self, string):
        self._file.write('%s\n' % string)

class FTPSource(object):
    implements(IFileSource)
    
    def __init__(self, folder=None, _filter=None, **kwargs):
        self.folder = folder
        self.filter = _filter
        self.ftp_kwargs = kwargs
    
    def __call__(self):
        """ Return an open file-like object containing the data """
        ftp = FTP(**self.ftp_kwargs)
        
        if not self.ftp_kwargs.get('user', None):
            ftp.login()
        
        temp_files = []
        
        if self.folder:
            ftp.cwd(self.folder)
        
        files = ftp.nlst()
        
        log.info(str(files))
        
        if self.filter:
            if isinstance(self.filter, basestring):
                try:
                    files = filter(re.compile(self.filter).match, files)
                except:
                    files = []
            elif callable(self.filter):
                files = filter(self.filter,files)
        
        log.info(str(files))
        
        for file in files:
            temp_file = TemporaryFile()
            cb = FileWriteCallback(temp_file)
            ftp.retrlines('RETR %s' % file, cb)
            temp_file.seek(0)
            temp_files.append(temp_file)
    
        try:
          ftp.quit()
        except:
          try:
            ftp.close()
          except:
            pass
        
        return temp_files